package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.MvcResult;
import com.stylefeng.guns.common.persistence.model.QualityGradeRule;
import com.stylefeng.guns.common.persistence.dao.QualityGradeRuleMapper;
import com.stylefeng.guns.modular.system.service.IQualityGradeRuleService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 学历等级、技术等级、职业资格等级 服务实现类
 * </p>
 *
 * @author jondai
 * @since 2018-01-03
 */
@Service
public class QualityGradeRuleServiceImpl extends ServiceImpl<QualityGradeRuleMapper, QualityGradeRule> implements IQualityGradeRuleService {

    @Autowired
    QualityGradeRuleMapper qualityGradeRuleMapper;

    @Override
    public void empty() {
        qualityGradeRuleMapper.empty();
    }

    @Override
    public void emptyByGradeType(String gradeType) {
        qualityGradeRuleMapper.emptyByGradeType(gradeType);
    }

    @Override
    public List<QualityGradeRule> selectAll() {
        return qualityGradeRuleMapper.selectAll();
    }

    @Override
    public List<QualityGradeRule> getByGradeType(String type) {

        QualityGradeRule qualityGradeRule = new QualityGradeRule();
        qualityGradeRule.setGradeType(type);
        EntityWrapper<QualityGradeRule> wrapper = new EntityWrapper<>();
        wrapper.setEntity(qualityGradeRule);
        wrapper.orderBy("grade_code");
        List<QualityGradeRule> qualityGradeRuleList = qualityGradeRule.selectList(wrapper);

        return qualityGradeRuleList;
    }

    @Override
    public Map<String, String> getMapGradeAndGradeCodeByType(String type) {
        List<QualityGradeRule> byGradeType = this.getByGradeType(type);
        Map<String,String> map = new HashMap<>();
        for (QualityGradeRule qualityGradeRule : byGradeType) {
            map.put(qualityGradeRule.getGrade(),qualityGradeRule.getGradeCode());
        }
        return map;
    }

    @Override
    public Map<String, Double> getMapGradeCodeAndIntegralByType(String type) {
        List<QualityGradeRule> byGradeType = this.getByGradeType(type);
        Map<String,Double> map = new HashMap<>();
        for (QualityGradeRule qualityGradeRule : byGradeType) {
            map.put(qualityGradeRule.getGradeCode(),qualityGradeRule.getIntegral());
        }
        return map;
    }


    @Override
    public MvcResult checkGradeCode(List<QualityGradeRule> proQualifyRuleList) {
        //TODO 检查编码是否重复 true：重复 false：不重复
        for (QualityGradeRule qualityGradeRule : proQualifyRuleList) {
            MvcResult mvcResult = checkGradeCode(qualityGradeRule);
            if("true".equals(mvcResult.getState())){
                return mvcResult;
            }
        }

        MvcResult mvcResult = new MvcResult();
        mvcResult.setState("false");
        return mvcResult;
    }

    /**
     * 检查该gradeCode是否已经存在于数据库中
     * @param proQualifyRule
     * @return
     */
    public MvcResult checkGradeCode(QualityGradeRule proQualifyRule) {
        MvcResult mvcResult = new MvcResult();
        String gradeCode = proQualifyRule.getGradeCode();
        Map<String,Object> condition = new HashMap<>();
        condition.put(QualityGradeRule.gradeCodeName, gradeCode);
        List<QualityGradeRule> qualityGradeRules = this.selectByMap(condition);

        if(qualityGradeRules != null && qualityGradeRules.size() == 0){
            mvcResult.setState("false");
        }else{
            mvcResult.setState("true");
            mvcResult.setMsg(proQualifyRule.getGrade() + "编码重复");
        }
        return mvcResult;
    }
}
